<?php
/*
 * Created on Apr 22, 2011
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
if(isset($_GET['item_id'])){
	$item_id = $_GET['item_id'];
}else{
	$item_id = 1;
}

if(isset($_GET['function'])){
	session_start();
	$function = $_GET['function'];
	if(strcmp($function,'delete') == 0){
		if(array_key_exists($item_id,$_SESSION['items'])){
			unset($_SESSION['items'][$item_id]);
		}
	}elseif(strcmp($function,'add') == 0){
		if(!isset($_SESSION['items']) || count($_SESSION['items']) == 0){
			$_SESSION['items'][$item_id] = 1;
		}else{
			if(array_key_exists($item_id,$_SESSION['items'])){
				$num = $_SESSION['items'][$item_id];
				$_SESSION['items'][$item_id] = $num + 1;
			}else{
				$_SESSION['items'][$item_id] = 1;
			}
		}
	}elseif(strcmp($function,'update') == 0){
		$_SESSION['items'][$item_id] = $_GET['qty'];
	}
}

$i = 0;
$result = null;
$item_total = isset($_SESSION['items']) ? count($_SESSION['items']) : 0;
if($item_total > 0){
	$item_temp = "";
	foreach($_SESSION['items'] as $key => $item){
		$item_temp .= $key;
		if($i < $item_total - 1){
			$item_temp .= ',';
		}
		$i++;
	}
	
	include "../../db/dbconnect.php";
	$query = 'SELECT item_id,name,price_buy,file,effects FROM item WHERE item_id in ('.$item_temp.')';
	$result = mysql_query($query);
	$cnt = mysql_num_rows($result);
}else{
	$cnt = 0;
}

if(!isset($_GET['function'])){
	generate_table($result,$cnt);
}else{
	// Construct XML tree.
	$xml = new DOMDocument();
	$xml->encoding = "UTF-8";
	$xml->formatOutput = true;
	
	$items = $xml->appendChild($xml->createElement('items'));
	$total = $items->appendChild($xml->createElement('total'));
	$total->appendChild($xml->createTextNode($cnt));
	
	if($cnt > 0){
		while($row = mysql_fetch_array($result))
	    {
	    	$item = $items->appendChild($xml->createElement('item'));
	    	$id = $item->appendChild($xml->createElement('id'));
	    	$id->appendChild($xml->createTextNode($row['item_id']));
	    	$name = $item->appendChild($xml->createElement('name'));
	    	$name->appendChild($xml->createTextNode($row['name']));
	    	$image = $item->appendChild($xml->createElement('image'));
	    	$image->appendChild($xml->createTextNode('../../images/shop/'.$row['file']));
	    	$price_buy = $item->appendChild($xml->createElement('price_buy'));
	    	$price_buy->appendChild($xml->createTextNode($row['price_buy']));
	    	$effects = $item->appendChild($xml->createElement('effects'));
	    	$effects->appendChild($xml->createTextNode($row['effects']));
	    	$quantity = $item->appendChild($xml->createElement('quantity'));
	    	$quantity->appendChild($xml->createTextNode($_SESSION['items'][$row['item_id']]));
	    }
	}
		
	header("Content-type: text/xml");
	echo $xml->saveXML();
}
?>

<?php
function generate_table($result,$cnt){?>
	<div id="simple_sc_div">
    	<table id="simple_sc_table">
    	<tr>
    	    <th class="simple_sc_item">Item</th>
    	    <th class="simple_sc_price">price</th>
    	    <th></th>
    	</tr>
		<?php
		if($cnt > 0){
			$total = 0;
			while($row = mysql_fetch_array($result)){
				$price = $row['price_buy'];
				$total = $total + $price;
				?>
				<tr><td class="simple_sc_item"><?php echo($row['name']);?>
				</td><td class="simple_sc_price"><?php echo($price);?>
				</td><td>
				<input type="image" src="../../images/shop/ico_remove.gif" onclick="deleteFromCart(<?php echo($row['item_id']);?>)"/>
				</td></tr>
			<?php
			}
		}else{
			$total = "-";
		}?>
		</table>
    </div>
	<div class="simple_sc_component">
	    <table id="simple_sc_total_table">
	    <tr>
	        <td class="simple_sc_item">Total: </td>
	        <td id="simple_sc_total_price" class="simple_sc_price"><?php echo($total);?></td>
	        <td></td>
	    </table>
	</div>
	<?php
}
?>
